export default {
  /** 
   * @type {String} 数据库名称
   */
  dbName: 'testDB',
  /**
   * 数据库地址
   * @type {String} 推荐以下划线为开头   _doc/xxx.db
   */
  dbPath: '_doc/testDB.db',
  /**
   * 用户表名
   */
  recordsTable: 'users',

  /**
   * 判断数据库是否打开
   */
  isOpen() {
    return plus.sqlite.isOpenDatabase({
      name: this.dbName,
      path: this.dbPath
    });
  },

  /**
   * 打开数据库
   */
  openSqlite() {
    return new Promise((resolve, reject) => {
      plus.sqlite.openDatabase({
        name: this.dbName,
        path: this.dbPath,
        success: (e)=>{
			console.log('e: ', e)
			//     this.db = this.dbName;
			// this.addLog(`数据库打开/创建成功: ${this.dbName}`, "success");
			//     this.updateDbStatus();
			resolve(e);
		},
        fail:(e)=> {
			// this.addLog(`数据库打开失败: ${JSON.stringify(e)}`, "error");
			if (e.code == -1402){
				resolve('重复打开')
			}else{
				reject(e);
			}
		}
      });
    });
  },

  /**
   * 关闭数据库
   */
  closeSqlite() {
    return new Promise((resolve, reject) => {
      plus.sqlite.closeDatabase({
        name: this.dbName,
        success: resolve,
        fail: reject
      });
    });
  },

  /**
   * 创建数据表
   * @param {String} dbTable 表名
   * @param {String} data 表结构
   */
  createTable(dbTable, data) {
    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({
        name: this.dbName,
        sql: `CREATE TABLE IF NOT EXISTS ${dbTable}(${data})`,
        success: resolve,
        fail: reject
      });
    });
  },

  /**
   * 删除数据表
   * @param {String} dbTable 表名
   */
  dropTable(dbTable) {
    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({
        name: this.dbName,
        sql: `DROP TABLE ${dbTable}`,
        success: resolve,
        fail: reject
      });
    });
  },

  /**
   * 插入数据
   * @param {String} dbTable 表名
   * @param {String} data 数据值
   * @param {String} [condition] 列名
   */
  insertTableData(dbTable, data, condition) {
    if (!dbTable || !data) {
      return Promise.reject("缺少必要参数");
    }

    let sql;
    if (condition) {
      sql = `INSERT INTO ${dbTable} (${condition}) VALUES(${data})`;
    } else {
      sql = `INSERT INTO ${dbTable} VALUES(${data})`;
    }

    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({
        name: this.dbName,
        sql: sql,
        success: resolve,
        fail: reject
      });
    });
  },

  // 其他方法保持类似结构...
  // 以下是精简后的其他核心方法（实现逻辑相同）

  /**
   * 查询数据
   * @param {String} dbTable 表名
   * @param {String} [condition] 查询条件
   */
  selectTableData(dbTable, condition = '') {
    return new Promise((resolve, reject) => {
      plus.sqlite.selectSql({
        name: this.dbName,
        sql: `SELECT * FROM ${dbTable} ${condition}`.trim(),
        success: resolve,
        fail: reject
      });
    });
  },

  /**
   * 删除数据
   * @param {String} dbTable 表名
   * @param {String} [condition] 删除条件
   */
  deleteTableData(dbTable, condition = '') {
    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({
        name: this.dbName,
        sql: `DELETE FROM ${dbTable} ${condition}`.trim(),
        success: resolve,
        fail: reject
      });
    });
  },

  /**
   * 更新数据
   * @param {String} dbTable 表名
   * @param {String} data 更新数据
   * @param {String} [condition] 更新条件
   */
  updateTableData(dbTable, data, condition = '') {
    let sql = `UPDATE ${dbTable} SET ${data}`;
    if (condition) {
      sql += ` WHERE ${condition}`;
    }

    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({
        name: this.dbName,
        sql: sql,
        success: resolve,
        fail: reject
      });
    });
  }
};